package com.investment.util.BaseDao;

/**
 * Created by veicth on 17/2/25.
 */

import com.investment.util.DaoException.DbException;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class MyBatisDao<T> {
    protected static final int DefaultPageNum = 10;
    private final String BEAN_NAME;

    public MyBatisDao() {
        Type genType = this.getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
        if(params != null & params.length > 0) {
            String bName = params[0].toString();
            if(bName != null && bName.startsWith("class ")) {
                this.BEAN_NAME = bName.substring(6);
            } else {
                this.BEAN_NAME = bName;
            }
        } else {
            this.BEAN_NAME = "";
        }

    }

    public String formatSqlId(String sqlId) throws DbException {
        if(sqlId == null) {
            throw new DbException("请配置sqlId值");
        } else {
            return sqlId.indexOf(".") == -1?this.BEAN_NAME + "." + sqlId:sqlId;
        }
    }
}
